﻿<UserControl x:Class="TanmiaGrp.GeneralLedger.WinApp.Scenarios.AccountBrowserView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:loc="clr-namespace:TanmiaGrp.GeneralLedger.WinApp"
             xmlns:Scenarios="clr-namespace:TanmiaGrp.GeneralLedger.WinApp.Scenarios"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mui="http://firstfloorsoftware.com/ModernUI"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="600"
             FlowDirection="RightToLeft"
             Name="vAccountBrowser">

    <UserControl.Resources>

        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <loc:BalanceToColorConverter x:Key="BalanceToColorConverter" />

        <ContextMenu x:Key="AccountContextMenu">
            <MenuItem Name="miEdit"
                      Header="تعديل"
                      Click="OnAccountContextMenuClicked"
                      CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Parent.PlacementTarget}" />
            <MenuItem Name="miAdd"
                      Header="اضافة حساب فرعي"
                      Click="OnAccountContextMenuClicked"
                      CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Parent.PlacementTarget}" />
        </ContextMenu>
    </UserControl.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <!-- Right Search Panel -->
        <Grid Name="searchBar" Grid.Column="0" Width="0">
            <Grid.Resources>
                <Style TargetType="{x:Type mui:ModernButton}">
                    <Setter Property="Width" Value="32" />
                    <Setter Property="Height" Value="32" />
                    <Setter Property="EllipseDiameter" Value="30" />
                    <Setter Property="Foreground" Value="#FFA1A1A1" />
                    <Setter Property="Margin" Value="2,0,0,0" />
                    <Setter Property="IconWidth" Value="12" />
                    <Setter Property="IconHeight" Value="12" />
                    <Setter Property="Background" Value="White" />
                    <Setter Property="BorderBrush" Value="#919191" />
                    <Setter Property="EllipseStrokeThickness" Value="2" />
                </Style>
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <DockPanel Grid.Row="0" Height="46">
                <TextBlock Text="البحث" Foreground="#FF6699C9" FontSize="22" VerticalAlignment="Center" Margin="4,0" />

                <!-- Close SearchPanel Button -->
                <mui:ModernButton ToolTip="خلف" FlowDirection="LeftToRight" IconData="{StaticResource Back}" HorizontalAlignment="Right" Margin="0,0,2,0">
                    <mui:ModernButton.Triggers>
                        <EventTrigger RoutedEvent="Button.Click">
                            <BeginStoryboard>
                                <Storyboard>
                                    <!-- Contract width of SearchBar -->
                                    <DoubleAnimation Storyboard.TargetName="searchBar"
                                                 Storyboard.TargetProperty="Width"
                                                 To="0"
                                                 Duration="0:0:.4" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </mui:ModernButton.Triggers>
                </mui:ModernButton>
            </DockPanel>

            <Grid Grid.Row="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <TextBox Name="txtSearch" 
                         Grid.Row="0"
                         Height="30" 
                         VerticalContentAlignment="Top" 
                         FontSize="16" 
                         Text="{Binding Path=SearchSpecification.InteractiveSearch, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Margin="4,2,4,0" />

                <CheckBox Grid.Row="1" 
                          Height="32" 
                          Foreground="Gray" 
                          Content="اظهار المجموعات الموقفة" 
                          Margin="4,0" 
                          VerticalAlignment="Top" 
                          IsChecked="{Binding Path=SearchSpecification.ShowIsDisabled, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
            </Grid>

            <DockPanel Grid.Row="2" Height="46">
                <mui:ModernButton ToolTip="تحديث البيانات"
                                  IconData="{StaticResource Refresh}"
                                  HorizontalAlignment="Right"
                                  Margin="0,0,2,0"
                                  Command="{Binding Path=RefreshCommand}" 
                                  CommandParameter="{x:Static loc:NavigationMoveEnum.KeepCurrent}"/>
            </DockPanel>

            <!-- separator between panels -->
            <Border Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" Width="1" Margin="2,0">
                <Border.Background>
                    <LinearGradientBrush StartPoint=".5,0" EndPoint=".5,1">
                        <GradientStop Color="#FFDADADA" Offset="0.0" />
                        <GradientStop Color="#919191" Offset=".8" />
                        <GradientStop Color="#919191" Offset=".2" />
                        <GradientStop Color="#FFE4E4E4" Offset="1.0" />
                    </LinearGradientBrush>
                </Border.Background>
                <Border.Effect>
                    <DropShadowEffect ShadowDepth="0" Direction="1" Opacity="0.395" Color="#FF919191"  />
                </Border.Effect>
            </Border>
        </Grid>

        <Grid Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="2" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <DockPanel Grid.Row="0" Height="46">
                <Scenarios:WindowBar x:Name="windowBar" PopouClicked="OnPopout" />
                <mui:ModernButton ToolTip="بحث" 
                                  IconData="{StaticResource Search}" 
                                  Width="32" Height="32" 
                                  EllipseDiameter="30" 
                                  EllipseStrokeThickness="2"
                                  Foreground="#FFA1A1A1" 
                                  Margin="2,0,0,0" 
                                  IconWidth="12" 
                                  IconHeight="12" 
                                  Background="White" 
                                  BorderBrush="#919191">
                    <mui:ModernButton.Triggers>
                        <EventTrigger RoutedEvent="Button.Click">
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="searchBar" 
                                                     Storyboard.TargetProperty="Width"
                                                     To="200"
                                                     Duration="0:0:.6" AccelerationRatio="1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </mui:ModernButton.Triggers>
                </mui:ModernButton>

                <TextBlock DockPanel.Dock="Left" 
                           Text="الحسابات" 
                           Style="{StaticResource CaptionStyle}"  />
                <Scenarios:AddBar HorizontalAlignment="Right" DockPanel.Dock="Right" />
                <Scenarios:SelectionModificationBar HorizontalAlignment="Right" Visibility="{Binding ElementName=dgGroups, Path=SelectedItem, Converter={StaticResource NullToVisibilityConverter}}" />
            </DockPanel>

            <ProgressBar Grid.Row="1" Minimum="0" Maximum="1" Height="2" IsIndeterminate="True" VerticalAlignment="Center" HorizontalAlignment="Stretch" 
                         Visibility="{Binding Path=EntitiesTask.IsNotCompleted, Converter={StaticResource BooleanToVisibilityConverter}}"/>


            <Grid Grid.Row="2">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="35*" />
                    <ColumnDefinition Width="5" />
                    <ColumnDefinition Width="65*" />
                </Grid.ColumnDefinitions>

                <Grid Grid.Column="0">
                    <TreeView x:Name="tvAccount"
                              ItemsSource="{Binding Path=EntitiesTask.Result.Entities}"
                              Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Scenarios:AccountBrowserView}}}"
                              PreviewMouseLeftButtonDown="OnAccountLeftButtonDown">
                        <TreeView.ItemContainerStyle>
                            <Style TargetType="TreeViewItem">
                                <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
                                <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
                                <Setter Property="FontWeight" Value="Normal" />
                                <Setter Property="ContextMenu" Value="{StaticResource AccountContextMenu}" />
                                <Style.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter Property="FontWeight" Value="Bold" />
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="False">
                                        <Setter Property="FontWeight" Value="Normal" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </TreeView.ItemContainerStyle>
                        <TreeView.ItemTemplate>
                            <HierarchicalDataTemplate ItemsSource="{Binding Path=AccountChildren}">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Path=AccountNumber}" />
                                    <TextBlock x:Name="d1"
                                               Text=" - " />
                                    <TextBlock Text="{Binding Path=AccountName}" />
                                </StackPanel>
                                <HierarchicalDataTemplate.Triggers>
                                    <DataTrigger Binding="{Binding Path=AccountID}"
                                                 Value="-1">
                                        <Setter TargetName="d1"
                                                Property="Visibility"
                                                Value="Collapsed" />
                                    </DataTrigger>
                                </HierarchicalDataTemplate.Triggers>
                            </HierarchicalDataTemplate>
                        </TreeView.ItemTemplate>
                    </TreeView>
                </Grid>

                <GridSplitter Width="1" Margin="2,4" Grid.Column="1" />

                <Grid Grid.Column="2">
                    <ListView Name="lvAccount"
                              ItemsSource="{Binding ElementName=tvAccount, Path=SelectedItem.AccountChildren}"
                              PreviewMouseLeftButtonDown="OnAccountLeftButtonDown">
                        <ListView.ItemContainerStyle>
                            <Style TargetType="ListViewItem"
                                   BasedOn="{StaticResource {x:Type ListViewItem}}">
                                <EventSetter Event="MouseDoubleClick"
                                             Handler="OnAccountDoubleClick" />

                                <Setter Property="ContextMenu"
                                        Value="{StaticResource AccountContextMenu}" />
                            </Style>
                        </ListView.ItemContainerStyle>
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="رقم الحساب"
                                                DisplayMemberBinding="{Binding Path=AccountNumber}" />
                                <GridViewColumn Header="اسم الحساب"
                                                DisplayMemberBinding="{Binding Path=AccountName}" />
                                <GridViewColumn Header="مجموع المدين"
                                                DisplayMemberBinding="{Binding Path=TotalDebit}" />
                                <GridViewColumn Header="مجموع الدائن"
                                                DisplayMemberBinding="{Binding Path=TotalCredit}" />
                                <GridViewColumn Header="الرصيد"
                                                DisplayMemberBinding="{Binding Path=Balance}" />
                            </GridView>
                        </ListView.View>

                    </ListView>
                    
                    <Grid Background="White" Visibility="{Binding ElementName=tvAccount, Path=SelectedItem.IsTransactional, Converter={StaticResource BooleanToVisibilityConverter}}">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="46" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        
                        <DockPanel Grid.Row="0">
                            <DockPanel.Resources>
                                <Style TargetType="TextBlock">
                                    <Setter Property="FontSize" Value="22" />
                                    <Setter Property="Foreground" Value="#FF6699C9" />
                                    <Setter Property="VerticalAlignment" Value="Center" />
                                </Style>
                            </DockPanel.Resources>
                            <TextBlock Text="كشف حساب / " Style="{StaticResource CaptionStyle}"  />
                            <TextBlock Text="{Binding Path=SelectedAccount.AccountNumber}" Style="{StaticResource CaptionStyle}" />
                            <TextBlock Text=" - " Style="{StaticResource CaptionStyle}" />
                            <TextBlock Text="{Binding Path=SelectedAccount.AccountName}" Style="{StaticResource CaptionStyle}" />
                        </DockPanel>
                        <DataGrid Grid.Row="1"
                                  ItemsSource="{Binding Path=StatementOfAccount}" 
                                  AutoGenerateColumns="False" 
                                  CanUserAddRows="False"
                                  CanUserDeleteRows="False"
                                  Background="White">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="المدين" Binding="{Binding Path=Debit}" IsReadOnly="True" />
                                <DataGridTextColumn Header="دائن" Binding="{Binding Path=Credit}" IsReadOnly="True" />
                                <DataGridTextColumn Header="التراكمي" ToolTipService.ToolTip="الرصيد التراكمي" Binding="{Binding Path=RunningBalance}">
                                    <DataGridTextColumn.ElementStyle>
                                        <Style TargetType="TextBlock">
                                            <Setter Property="Background" Value="{Binding Path=RunningBalance, Converter={StaticResource BalanceToColorConverter}}" />
                                        </Style>
                                    </DataGridTextColumn.ElementStyle>
                                </DataGridTextColumn>
                                <DataGridTextColumn Header="وصف" Binding="{Binding Path=Description}" IsReadOnly="True" />
                                <DataGridTextColumn Header="نوع" Binding="{Binding Path=Voucher.Source}" IsReadOnly="True" />
                                <DataGridTextColumn Header="التاريخ" Binding="{Binding Path=CreateDate}" IsReadOnly="True" />
                            </DataGrid.Columns>
                        </DataGrid>
                    </Grid>
                </Grid>
            </Grid>
        </Grid>
    </Grid>
</UserControl>
